package com.netease.nimlib.ipc.cp.protocol;

import android.content.UriMatcher;
import android.net.Uri;
import android.util.SparseArray;

/**
 * URI协议定义
 * uri=content://{authority}/{path}
 * path=/{type}/{name}/{key}
 * uri匹配器 content://{authority}/{type}/{name}/{key}
 * <p>
 * Created by huangjun on 2017/10/31.
 */

public final class Protocol {

    /// static final class
    private Protocol() {
    }

    /// TAG
    public static final String TAG = "CP";

    /// {authority}
    private static final String AUTHORITY = "com.netease.nimlib.ipc.provider";

    /// uri prefix: content://{authority}/{type}/
    private static final String URI_PREFIX_BOOLEAN = "content://" + AUTHORITY + "/boolean/";
    private static final String URI_PREFIX_STRING = "content://" + AUTHORITY + "/string/";
    private static final String URI_PREFIX_INT = "content://" + AUTHORITY + "/integer/";
    private static final String URI_PREFIX_LONG = "content://" + AUTHORITY + "/long/";
    private static final String URI_PREFIX_FLOAT = "content://" + AUTHORITY + "/float/";
    private static final String URI_PREFIX_VOID = "content://" + AUTHORITY + "/void/";

    /// {type}
    public static final int URI_TYPE_STRING = 1;
    public static final int URI_TYPE_BOOLEAN = 2;
    public static final int URI_TYPE_INT = 3;
    public static final int URI_TYPE_LONG = 4;
    public static final int URI_TYPE_FLOAT = 5;
    public static final int URI_TYPE_VOID = 6;

    /// uri matcher
    private static final UriMatcher sUriMatcher;
    private static final SparseArray<String> sType2UriPrefixArray;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, "string/*/*", URI_TYPE_STRING);
        sUriMatcher.addURI(AUTHORITY, "boolean/*/*", URI_TYPE_BOOLEAN);
        sUriMatcher.addURI(AUTHORITY, "integer/*/*", URI_TYPE_INT);
        sUriMatcher.addURI(AUTHORITY, "long/*/*", URI_TYPE_LONG);
        sUriMatcher.addURI(AUTHORITY, "float/*/*", URI_TYPE_FLOAT);
        sUriMatcher.addURI(AUTHORITY, "void/*/*", URI_TYPE_VOID);

        sType2UriPrefixArray = new SparseArray<>(6);
        sType2UriPrefixArray.put(URI_TYPE_STRING, URI_PREFIX_STRING);
        sType2UriPrefixArray.put(URI_TYPE_BOOLEAN, URI_PREFIX_BOOLEAN);
        sType2UriPrefixArray.put(URI_TYPE_INT, URI_PREFIX_INT);
        sType2UriPrefixArray.put(URI_TYPE_LONG, URI_PREFIX_LONG);
        sType2UriPrefixArray.put(URI_TYPE_FLOAT, URI_PREFIX_FLOAT);
        sType2UriPrefixArray.put(URI_TYPE_VOID, URI_PREFIX_VOID);
    }

    /// {key}
    public static final String URI_KEY = "key";

    /// {value}
    public static final String URI_VALUE = "value";

    /// {notify}
    public static final String URI_NOTIFY = "notify";

    /// cursor column
    public static final String[] CURSOR_COLUMN_NAMES = {Protocol.URI_VALUE};

    /// build uri
    public static Uri buildUri(final String name, final String key, final int type) {
        return Uri.parse(sType2UriPrefixArray.get(type) + name + "/" + key); // uri=content://authority/string/contact/hj
    }

    /// match uri
    public static int matchUri(Uri uri) {
        return sUriMatcher.match(uri);
    }

    /// uri name/key wrapper
    public static class KeyInfo {
        private String name;
        private String key;

        public KeyInfo(String name, String key) {
            this.name = name;
            this.key = key;
        }

        public String getName() {
            return name;
        }

        public String getKey() {
            return key;
        }

        @Override
        public String toString() {
            return "KeyInfo{" +
                    "name='" + name + '\'' +
                    ", key='" + key + '\'' +
                    '}';
        }
    }
}
